package com.ghsc.ghxc.jsb.ui.live.activity

import android.os.Bundle
import com.alivc.auimessage.listener.InteractionCallback
import com.alivc.auimessage.model.base.InteractionError
import com.aliyun.auiappserver.AppServerApi
import com.aliyun.auiappserver.model.GetLiveRequest
import com.aliyun.auiappserver.model.LiveModel
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.loge
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.utils.GsonUtil
import com.cn.appcore.utils.TimeUtils
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.config.UserManager
import com.ghsc.ghxc.jsb.databinding.ActivityLieveEndBinding

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.live.activity
 * 类名：LiveEndStopActivity
 * 创建时间：2024/5/10 18:03
 * 创建人 @author： xiaolin
 * 描述 ：Todo... 直播结束
 */
class LiveEndStopActivity  : BaseVMActivity<LiveEndStopViewModel, ActivityLieveEndBinding>(){
    override fun attachLayoutRes(): Int {
        return R.layout.activity_lieve_end
    }



    override fun attachVMClass(): Class<LiveEndStopViewModel>? {
        return LiveEndStopViewModel::class.java
    }

    var  bundle : Bundle?=null
    var liveId:String =""
    override fun initBar() {
        super.initBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }
    override fun initView() {
        bundle=intent.extras

        liveId=bundle!!.getString("liveId", "")
        binding.bar.topLeftView.setSingleClickListener { finish() }
        binding.bar.topTitle.text=""


        val request = GetLiveRequest()
        request.id = liveId
        request.userId = UserManager.getUserId()
        loge("直播结束="+request.toString())
        AppServerApi.instance().getLive(request)
            .invoke(object : InteractionCallback<LiveModel> {
                override fun onSuccess(liveModel: LiveModel) {
                    loge("直播结束="+liveModel)
//                    binding.tvCount.text=(liveModel.metrics.pv * LiveBlackList.numberMultiple).toString()
                    var start : Long = TimeUtils.dateUTCToStr(liveModel.createdAt)
                    var end   : Long = TimeUtils.dateUTCToStr(liveModel.stopped_at)
                    loge("直播结束开始时间="+liveModel.createdAt)
                    loge("直播结束结束时间="+liveModel.stopped_at)
                    var times= end -start//这样得到的差值是微秒级别
                    val days: Int = TimeUtils.getCurrentDays(times)
                    val hour: Int = TimeUtils.getCurrentHour(times)
                    val minute: Int = TimeUtils.getCurrentMinute(times)
                    val mills: Int = TimeUtils.getCurrentMills(times)
                    val mCurrentTimeStr =  days.toString() + "天" +
                            hour + "小时" +
                            minute + "分" +
                            mills + "秒"
                    binding.tvStartDate.text= "直播时长: $mCurrentTimeStr"
//        val minutes = end - start

                }


                override fun onError(interactionError: InteractionError?) {

                }
            })


        //直播_获取直播间的打赏礼物信息
        val map1: HashMap<String, Any> = HashMap()
        map1["roomid"] = liveId
        GsonUtil.mapToJsonStrObjEncryption(map1)?.let { it1 -> mViewModel.onLiveEndMethod(it1) }



    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@LiveEndStopActivity) {
                showToast(it)
            }
            againLoginMsg.observe(this@LiveEndStopActivity) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }

            onLiveEndData.observe(this@LiveEndStopActivity){
                binding.run {
                    tvCount.text=it.data.data.likes.toString()
                    numCount.text=it.data.data.totalsendcnt.toString()
                    userCount.text=it.data.data.totalsenduid.toString()
                    jifenCount.text=it.data.data.totalsendprice.toString()
                }
            }
        }
    }
}